<script type="application/javascript" src="/mall2/js/cart.js?_t={time()}"></script>

<div id="cartOne" class="sku_dialog" style=" display:none;">
    <div class="widget_mask" onClick="$('#cartOne').hide()"></div>
    <div class="widget_wrap2">
        <div class="widget_wraps">
            <div class="empty_prd">
                <div class="empty_prd_qx" onClick="$('#cartOne').hide()">关闭</div>
                <div class="empty_prd_qk" onclick="clearCart()">一键清空</div>
            </div>
            <div class="car_prd">
                <ul>
                    {if $cart_list}
                        {foreach from=$cart_list item=item name=foo}
                            <li>
                                <div class="mall_bt_over">
                                    <div class="prd_tlt_s">{$item.title}{if $item.spec_str}[{$item.spec_str}]{/if}<em>￥{$item.price_new}</em>
                                        <div class="prd_add_a">
                                            <div class="add_a_s quan" onclick="addCart({$item.pid}, 99)">-</div>
                                            <div class="add_a_sz">{$item.cart_num}</div>
                                            <div class="add_a_j quan" onclick="addCart({$item.pid}, 1)">+</div>
                                        </div>
                                    </div>
                                </div>
                            </li>
                        {/foreach}
                    {/if}
                </ul>
            </div>
        </div>
        <div class="fixed_cart_lf">
            <div class="cart_jse">
                <div class="cart_jgs_b">￥{$cart_list_price}</div>
                <div class="cart_jgb_b">{*已优惠<em>￥10</em>*}</div>
            </div>
        </div>
        <div class="fixed_cart_rg">
            {if $cart_list_price >= $send_low_money}
                <a href="/order/submit" class="fix_rg_dg_ok">结算</a>
            {else}
                <a href="javascript:;" class="fix_rg_dg">还差{$cart_list_price_sx}元起送</a>
            {/if}
        </div>
    </div>
</div>

<script type="text/javascript" language="JavaScript">
    $(function () {
        $('.add_gou').click(function () {
            var count = $(this).data('count');
            if (count > 1) {
                var cart = new Cart($(this), function (pid, num) {
                    addCart(pid, num);
                    $.toast('加入购物车成功！');
                });
                cart.create();
            } else {
                var pid = $(this).data('pid');
                addCart(pid, 1);
                $.toast('加入购物车成功！');
            }
        });
        loadCart();
    });

    function clearCart() {
        $.ajax({
            type: 'get',
            url: '/index/clearCart?r=' + Math.random(),
            async: false,
            success: function (data) {
                loadCart();
                $('#cartOne').hide();
                $('.empty_cart').show();
                $('.pro_cart').hide();
                $.toast('清空成功');
            }
        })
    }

    function addCart(id, num) {
        $.ajax({
            type: 'get',
            url: '/index/modifyCart?id=' + id + '&num=' + num + '&r=' + Math.random(),
            async: false,
            success: function (data) {
                loadCart();
            }
        })
    }

    function loadCart() {
        var qs_price = {$send_low_money};
        $.ajax({
            type: 'get',
            url: '/index/loadCart?r=' + Math.random(),
            async: false,
            success: function (data) {
                var str = '', price = 0, cart_count = 0;
                if (data['data']) {
                    $.each(data['data'], function (i, item) {
                        str += '<li>';
                        str += '<div class="mall_bt_over">';
                        str += '<div class="prd_tlt_s">' + item.title + (item.spec_str?('['+item.spec_str+']'):'') + '<em>￥' + item.price_new + '</em>';
                        str += '<div class="prd_add_a">';
                        str += '<div class="add_a_s quan" onclick="addCart(' + item.pid + ', 99)">-</div>';
                        str += '<div class="add_a_sz">' + item.cart_num + '</div>';
                        str += '<div class="add_a_j quan" onclick="addCart(' + item.pid + ', 1)">+</div>';
                        str += '</div>';
                        str += '</div>';
                        str += '</div>';
                        str += '</li>';

                        price += item.price_new * item.cart_num;
                        cart_count += item.cart_num;
                    });
                    //更新购物车当前不是同一个商品数量
                    $('.shot_n_i3').html(cart_count);
                    //更新当前已选商品的价格
                    $('.cart_jgs_b').html('￥' + price);
                    $('.pro_cart_price font').text(price);
                    $('.pro_cart_tips').html(data['tips']);
                    //更新起送价格
                    if (price >= qs_price) {
                        $('.fixed_cart_rg').html('<a href="/order/submit" class="fix_rg_dg_ok">结算</a>');
                    } else {
                        $('.fixed_cart_rg').html('<a href="javascript:;" class="fix_rg_dg">还差' + (qs_price - price) + '元起送</a>');
                    }
                    //更新购物车数据
                    $('.car_prd ul').html(str);
                    $('.empty_cart').hide();
                    $('.pro_cart').show();
                } else {
                    //回归
                    $('.shot_n_i3').html('0');
                    $('.cart_jgs_b').html('￥0');
                    $('.fixed_cart_rg').html('<a href="javascript:;" class="fix_rg_dg">还差' + qs_price + '元起送</a>');
                    $('.car_prd ul').html('');
                    $('.empty_cart').show();
                    $('.pro_cart').hide();
                }
            }
        });
    }
    loadCart();
</script>